package com.penghk.earth.util;

import java.io.*;
import java.util.Properties;

import com.jcraft.jsch.*;

public class ShellUtil {
    private static JSch jsch;
    private static Session session;


    /**
     * 连接到指定的IP
     *
     * @throws JSchException
     */
    public static void connect(String user, String passwd, String host) throws JSchException {
        jsch = new JSch();
        session = jsch.getSession(user, host, 22);
        session.setPassword(passwd);

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        session.connect();
    }

    /**
     * 执行相关的命令
     * @throws JSchException
     */
    public static void execCmd(String command, String user, String passwd, String host) throws JSchException {
        connect(user, passwd, host);

        BufferedReader reader = null;
        Channel channel = null;

        try {
            while (command != null) {
                channel = session.openChannel("exec");//sfpt | shell | exec
                ((ChannelExec) channel).setCommand(command);

                channel.setInputStream(null);
                ((ChannelExec) channel).setErrStream(System.err);

                channel.connect();
                InputStream in = channel.getInputStream();
                reader = new BufferedReader(new InputStreamReader(in));
                String buf = null;
                while ((buf = reader.readLine()) != null) {
                    System.out.println(buf);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSchException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            channel.disconnect();
            session.disconnect();
        }
    }

    /**
     * 执行文件下载
     * @param filePath
     * @param user
     * @param passwd
     * @param host
     * @throws JSchException
     */
    public static void execsftp(String filePath , String user , String passwd, String host) throws JSchException {
        connect(user, passwd, host);

        BufferedReader reader = null;
        Channel channel = null;

        try {
            channel = session.openChannel("sftp");//sfpt | shell | exec
            channel.connect(1000);
            ChannelSftp sftp = (ChannelSftp) channel;
            // 从服务器上下载文件到本地downloads文件夹
            InputStream inStream = sftp.get(filePath);
            OutputStream outStream = null;
            try {
                File targetFile = new File("/home/penghk/newFile.xml");
                outStream = new FileOutputStream(targetFile);
                byte b[] = new byte[1024];
                while (inStream.read(b) > 0) {
                    outStream.write(b);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (outStream != null) {
                    outStream.close();
                }
                if (inStream != null) {
                    inStream.close();
                }
            }
        }catch (Exception e) {

        }
    }

}


